#include "Socket.hpp"
#include "Protocol.hpp"
#include "InetAddr.hpp"
#include "Command.hpp"
#include <iostream>
#include <memory>

using namespace SocketModule;

void GetDataFromStdIn(int *x, int *y, char *oper)
{
    std::cout << "please enter X:";
    std::cin >> *x;
    std::cout << "please enter Y:";
    std::cin >> *y;
    std::cout << "please enter OPER:";
    std::cin >> *oper;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cout << "输入错误,请输入:" << "./tcpclient serveip serveport" << std::endl;
        exit(USAGE_ERR);
    }

    uint16_t port = std::atoi(argv[2]);
    std::string serve_ip = argv[1];

    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->BulidTcpClienttMethod();
    if (client->Connect(serve_ip, port) != 0)
    {
        exit(CONNECT_ERR);
    };

    std::unique_ptr<Protocol> pro = std::make_unique<Protocol>();
    std::string resp_buffer;
    while (true)
    {
        std::string send_str;
       
            int x, y;
            char oper;
            GetDataFromStdIn(&x, &y, &oper);
            // 协议化数据
            send_str += pro->BuildRequestString(x, y, oper);
            std::cout << "-----------encode req string-------------" << std::endl;
            std::cout << send_str << std::endl;
            std::cout << "------------------------------------------" << std::endl;
     

        // fasong
        client->Send(send_str);
        
        
            sleep(2);
            Response resp;
            bool ret = pro->GetResponse(client, resp_buffer, &resp);
            if (ret == false)
                break;
     
    }
    client->Close();
    return 0;
}